//
//  Emoticon.swift
//  MyWeibo
//
//  Created by cat on 15/8/6.
//  Copyright © 2015年 cat. All rights reserved.
//

import UIKit

class EmoticonPackage: NSObject {
    /// 目录名
    var id: String
    /// 分组名
    var groupName: String = ""
    /// 表情数组
    var emoticons: [Emoticon]?
    
    init(id: String, groupName: String = "") {
        self.id = id
        self.groupName = groupName
    }
    
    class func addFavorate(emoticon: Emoticon) {
        // 拿到第0个分组包中得数组
        var ems = packages[0].emoticons
        ems?.removeLast()
        
        emoticon.times++
        
        let contains = ems!.contains(emoticon)
        
        if !contains {
            ems!.append(emoticon)
            
        }
        
        ems = ems!.sort({ (e1, e2) -> Bool in
            return e1.times > e2.times
        })
        
        if !contains {
            ems?.removeLast()
        }
        
        ems?.append(Emoticon(remove: true))
        
        packages[0].emoticons = ems
        
    }
    
    
    
    static let packages = EmoticonPackage.loadPackages()
    
    private class func loadPackages() -> [EmoticonPackage] {
        
        // 加载路径
        let path = bundlePath.stringByAppendingPathComponent("emoticons.plist")
        // 加载plist
        let dict = NSDictionary(contentsOfFile: path)!
        let array = dict["packages"] as! [[String: AnyObject]]
        
        var arrayM = [EmoticonPackage]()
        
        let package = EmoticonPackage(id: "", groupName: "最近").appendEmptyEmotions()
        arrayM.append(package)
        
        for d in array {
            let id = d["id"] as! String
            
            let package = EmoticonPackage(id: id).loadEmoticons().appendEmptyEmotions()
            
            arrayM.append(package)
            
        }
        return arrayM
    }
    
    private func loadEmoticons() ->Self {
        // 路径
        let path = EmoticonPackage.bundlePath.stringByAppendingPathComponent(id).stringByAppendingPathComponent("info.plist")
        
        let dict = NSDictionary(contentsOfFile: path)!
        
        groupName = dict["group_name_cn"] as! String
        let array = dict["emoticons"] as! [[String: String]]
        
        emoticons = [Emoticon]()
        
        var index = 0
        for d in array {
            // 将表情的id 传递给表情
            emoticons?.append(Emoticon(id: id, dict: d))
            index++
            
            if index == 20 {
                emoticons?.append(Emoticon(remove: true))
                index = 0
                
            }
        }
        
        return self
    }

    private func appendEmptyEmotions() -> Self {
        
        if emoticons == nil {
            emoticons = [Emoticon]()
        }
        
        let count = emoticons!.count % 21
        
        
        if count > 0 || emoticons!.count == 0 {
        
        for _ in count..<20 {
        
        emoticons?.append(Emoticon(remove: false))
        }
        
        emoticons?.append(Emoticon(remove: true))
        }
        return self
    }
    
    // 表情包路径
    private static let bundlePath = NSBundle.mainBundle().bundlePath.stringByAppendingPathComponent("Emoticons.bundle")
    override var description: String {
        return "\(id)\(groupName)\(emoticons)"
    }
    
}

class Emoticon: NSObject {
    /// 表情目录
    var id: String?
    /// 发送给服务器的表情文字
    var chs: String?
    /// 本地显示的图片文件名
    var png: String?
    /// 图片完整路径
    var imagePath: String {
        if chs == nil {
            return ""
        }
        
        return EmoticonPackage.bundlePath.stringByAppendingPathComponent(id!).stringByAppendingPathComponent(png!)
    }
    
    var code: String? {
        didSet {
            let scanner = NSScanner(string: code!)
            
            var value: UInt32 = 0
            scanner.scanHexInt(&value)
//          
            emoji = String(Character(UnicodeScalar(value)))
        }
    }
    /// emoji 字符串
    var emoji: String?
    /// 删除表情标记
    var removeEmoticon = false
    
    var times = 0
    
    init(remove: Bool) {
        
        removeEmoticon = remove
    }
    
    init(id: String, dict: [String: String]) {
        self.id = id
        
        super.init()
        
        setValuesForKeysWithDictionary(dict)
    }
    
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {}
    
    override var description: String {
        return "\(chs)\(png)\(code)\(removeEmoticon)"
    }
}
